﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;

namespace MVCExtension.FrameWork
{
    public class StringHelper
    {  /// <summary>
        /// 取字符左函数
        /// </summary>
        /// <param name="Object">要操作的 string  数据</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string Left(object Object, int MaxLength)
        {
            if (IsNull(Object)) return "";
            return Object.ToString().Substring(0, Math.Min(Object.ToString().Length, MaxLength));
        }
        /// <summary>
        /// 取字符中间函数
        /// </summary>
        /// <param name="Object">要操作的 string  数据</param>
        /// <param name="StarIndex">开始的位置索引</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string GetSubString(string Object, int StarIndex, int MaxLength)
        {
            if (IsNull(Object)) return "";
            if (StarIndex >= Object.Length) return "";
            return Object.Substring(StarIndex, MaxLength);
        }
        /// <summary>
        /// 截取字符串函数
        /// </summary>
        /// <param name="Str">所要截取的字符串</param>
        /// <param name="Num">截取字符串的长度</param>
        /// <param name="Num">截取字符串后省略部分的字符串</param>
        /// <returns></returns>
        public static string GetSubString(string Str, int Num, string LastStr)
        {
            return (Str.Length > Num) ? Str.Substring(0, Num) + LastStr : Str;
        }
        /// <summary>
        /// 取字符右函数
        /// </summary>
        /// <param name="Object">要操作的 string  数据</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string Right(object Object, int MaxLength)
        {
            if (IsNull(Object)) return "";
            int i = Object.ToString().Length;
            if (i < MaxLength) { MaxLength = i; i = 0; } else { i = i - MaxLength; }
            return Object.ToString().Substring(i, MaxLength);
        }
        #region 基础判别函数
        /// <summary>
        /// 对象是否非空
        /// 为空返回 false
        /// 不为空返回 true
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <returns>bool值</returns>
        public static bool NotNull(object Object) { return !IsNull(Object, false); }
        /// <summary>
        /// 对象是否非空
        /// 为空返回 false
        /// 不为空返回 true
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="IsRemoveSpace">是否去除空格</param>
        /// <returns>bool值</returns>
        public static bool NotNull(object Object, bool IsRemoveSpace) { return !IsNull(Object, IsRemoveSpace); }
        /// <summary>
        /// 对象是否为空
        /// 为空返回 false
        /// 不为空返回 true
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <returns>bool值</returns>
        public static bool IsNull(object Object) { return IsNull(Object, false); }
        /// <summary>
        /// 对象是否为空
        /// 为空返回 false
        /// 不为空返回 true
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="IsRemoveSpace">是否去除空格</param>
        /// <returns>bool值</returns>
        public static bool IsNull(object Object, bool IsRemoveSpace)
        {
            if (Object == null) return true;
            string Objects = Object.ToString();
            if (Objects == "") return true;
            if (IsRemoveSpace)
            {
                if (Objects.Replace(" ", "") == "") return true;
                if (Objects.Replace("　", "") == "") return true;
            }
            return false;
        }




        #endregion
        /// <summary>
        ///  将字符转化为HTML编码
        /// </summary>
        /// <param name="str">待处理的字符串</param>
        /// <returns></returns>
        public static string HtmlEncode(string Input)
        {
            return System.Web.HttpContext.Current.Server.HtmlEncode(Input);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public static string HtmlDecode(string Input)
        {
            return System.Web.HttpContext.Current.Server.HtmlDecode(Input);
        }

        /// <summary>
        /// URL地址编码
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public static string URLEncode(string Input)
        {
            return System.Web.HttpContext.Current.Server.UrlEncode(Input);
        }

        /// <summary>
        /// URL地址解码
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public static string URLDecode(string Input)
        {
            return System.Web.HttpContext.Current.Server.UrlDecode(Input);
        }
        /// <summary>
        /// 去除html标签
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DealHtml(string str)
        {
            str = Regex.Replace(str, @"\<(img)[^>]*>|<\/(img)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(table|tbody|tr|td|th|)[^>]*>|<\/(table|tbody|tr|td|th|)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(div|blockquote|fieldset|legend)[^>]*>|<\/(div|blockquote|fieldset|legend)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(font|i|u|h[1-9]|s)[^>]*>|<\/(font|i|u|h[1-9]|s)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(style|strong)[^>]*>|<\/(style|strong)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(script)[^>]*>|<\/(script)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<a[^>]*>|<\/a>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(meta|iframe|frame|span|tbody|layer)[^>]*>|<\/(iframe|frame|meta|span|tbody|layer)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<br[^>]*", "", RegexOptions.IgnoreCase);
            return str.Replace("&nbsp;", "");
        }


        /// <summary>
        /// 去除script和A超链接
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DealScript(string str)
        {
            str = Regex.Replace(str, @"\<a[^>]*>|<\/a>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(meta|iframe|frame|span|tbody|layer)[^>]*>|<\/(iframe|frame|meta|span|tbody|layer)>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"\<(script)[^>]*>|<\/(script)>", "", RegexOptions.IgnoreCase);
            return str;
        }

        /// <summary>
        /// 去除<a></a>
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DealA(string str)
        {
            str = Regex.Replace(str, @"\<a[^>]*>|<\/a>", "", RegexOptions.IgnoreCase);
            return str.Replace("&nbsp;", "");
        }





        #region 字符操作过滤SQL

        /**/
        /// <summary>
        /// 过滤SQL
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string FilterSQL(string text)
        {
            string validSql = "";
            if (text != null)
            {
                text = text.Trim();
                text = text.Replace('\'', ' ');
                text = text.Replace("&nbsp;", "");
                text = text.Replace("\n;", "");
                text = text.Replace(";", "'';''");
                text = text.Replace("'", "''");
                text = text.Replace("--", "''--''");
                text = text.Replace("%25", "");
                text = text.Replace("%0a", "");
                text = text.Replace("%22", "");
                text = text.Replace("%27", "");
                text = text.Replace("%5c", "");
                text = text.Replace("%2f", "");
                text = text.Replace("%3c", "");
                text = text.Replace("%3e", "");
                text = text.Replace("%26", "");
                text = text.Replace("<", "&lt;");
                text = text.Replace(">", "&gt;");
                validSql = text;
            }
            return validSql;
        }



        public static string SafeRequest(string str)
        {
            string validSql = "";
            if (str != null)
            {
                str = str.Trim();
                str = str.Replace('\'', ' ');

                str = str.Replace("&nbsp;", "");
                str = str.Replace("\n;", "");
                validSql = str;
            }
            return validSql;
        }

        #endregion




        #region 数组操作
        /// <summary>
        /// 将字符串转换为ArrayList
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="sep">分隔符</param>
        /// <returns></returns>
        public static ArrayList String2ArrayList(string str, char sep)
        {
            ArrayList al = new ArrayList();
            string[] temp = str.Split(sep);
            foreach (string strings in temp)
            {
                al.Add(strings);
            }

            return al;
        }

        #endregion


        #region 时间格式化
         public static string getFormatdDateTime(DateTime dt)
         {
             return dt.ToString("yyyy-MM-dd HH:mm:ss");
         }
        #endregion
    }
}
